﻿using BwTMusicModels;
using HelperModels;
using HwjUIDI.Utility.DatabaseHelper;
using Microsoft.SqlServer.Server;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BwTServices.Order
{
    public class OrderServices
    {
        /// <summary>
        /// 获取所有信息
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<North_Order> GetAllOrders(OrderFilter filter, out int total)
        {
            total = 0;
            List<North_Order> orders = new List<North_Order>();
            total = 0;
            try
            {
                if (filter != null)
                {
                    if (filter.PageSize > 0 && filter.CurrentPage > 0)
                    {
                        StringBuilder sbSQL = new StringBuilder();
                        List<SqlParameter> listParam = new List<SqlParameter>();
                        sbSQL.Append("select top ");
                        sbSQL.Append(filter.PageSize);
                        sbSQL.Append(" * from (select count(1) over() as totalnumber, ROW_NUMBER() over(order by A.OrderTime) as rownumber,A.OrderId ,A.OrderTime,A.OrderCustomer,A.OrderState,A.OrderKey,A.OrderItem, A.OrderNote,A.OrderType from North_Order A  where '1' = '1'");
                        if (!string.IsNullOrEmpty(filter.CustomerName))
                        {
                            sbSQL.Append(" and A.OrderCustomer like '%' + @Name + '%'");
                            SqlParameter param = new SqlParameter("@Name", SqlDbType.NVarChar);
                            param.Value = filter.CustomerName;
                            listParam.Add(param);
                        }
                        if (!string.IsNullOrEmpty(filter.OrderState))
                        {
                            sbSQL.Append(" and A.OrderState= @State ");
                            SqlParameter param = new SqlParameter("@State", SqlDbType.NVarChar);
                            param.Value = filter.OrderState;
                            listParam.Add(param);
                        }
                        if (!string.IsNullOrEmpty(filter.OrderBeginTime))
                        {
                            sbSQL.Append(" and A.OrderTime> @Begin ");
                            SqlParameter param = new SqlParameter("@Begin", SqlDbType.DateTime);
                            param.Value =Convert.ToDateTime(filter.OrderBeginTime).ToString("yyyy-MM-dd HH:mm:ss");
                            listParam.Add(param);
                        }
                        if (!string.IsNullOrEmpty(filter.OrderEndTime))
                        {
                            sbSQL.Append(" and A.OrderTime<@End ");
                            SqlParameter param = new SqlParameter("@End", SqlDbType.DateTime);
                            param.Value = Convert.ToDateTime(filter.OrderEndTime).ToString("yyyy-MM-dd HH:mm:ss");
                            listParam.Add(param);
                        }
                        sbSQL.Append(") WTX where rownumber > (");
                        sbSQL.Append((filter.CurrentPage - 1) * filter.PageSize);
                        sbSQL.Append(")");
                        using (SqlDataReader dr = DatabaseFactory.GetDatabase().ExecuteDataReaderSQL(sbSQL.ToString(), listParam.ToArray()))
                        {
                            while (dr.Read())
                            {
                                int iIndex = 0;
                                North_Order list = new ModelsHelper().GetOrderModels(dr);
                                orders.Add(list);
                                if (iIndex == 0)
                                {
                                    total = DatabaseFactory.GetDatabase().GetInt(dr, "totalnumber");
                                }
                                iIndex++;
                            }
                        }

                    }

                }

            }
            catch (Exception ex)
            {

            }
            return orders;
        }
        /// <summary>
        /// 员工处理订单记录
        /// </summary>
        /// <returns></returns>
        public int RecodeOrderInfo(Guid OrderID,string userId)
        {
            int bResult = 0;
            try
            {
                string sql = $"update North_Order set OrderState=1 where OrderId='{OrderID}'";
                var result = DatabaseFactory.GetDatabase().ExecNonQuerySQL(sql);
                if (result > 0)
                {
                    string sSql = $"insert into DealOrder (DealManagerId,DealOrderId,DealAdmin) values('{Guid.NewGuid()}','{OrderID}','0df976a7-b339-4112-a2a6-cc166ed18075')";
                    var falg = DatabaseFactory.GetDatabase().ExecNonQuerySQL(sSql);
                    if (falg > 0)
                    {
                        bResult = falg;
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return bResult;
        }
        /// <summary>
        /// 判断此订单是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public int SearchOrderIsExist(Guid key)
        {
            int count = 0;
            try
            {
                string sql = $"select count(*) from North_Order where OrderKey='{key}'";
                count =Convert.ToInt32( DatabaseFactory.GetDatabase().ExecuteDataScalarSQL(sql));
            }
            catch (Exception)
            {

            }
            return count;
        }

        public int FinishDealOrder(Guid OrderId, string userId)
        {
            int bResult = 0;
            try
            {
                string sql = $"update North_Order set OrderState=2 where OrderId='{OrderId}'";
                var result = DatabaseFactory.GetDatabase().ExecNonQuerySQL(sql);
                if (result > 0)
                {
                    string sSql = $"update DealOrder set Status='Y' where DealOrderId='{OrderId}' and DealAdmin='0df976a7-b339-4112-a2a6-cc166ed18075'";
                    var falg = DatabaseFactory.GetDatabase().ExecNonQuerySQL(sSql);
                    if (falg > 0)
                    {
                        bResult = falg;
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return bResult;
        }
    
    }
}
